package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.enjity.PageResult;
import com.itheima.enjity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service( interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
//新增检查组，操作t_checkgroup表
        checkGroupDao.add(checkGroup);
//设置检查组与检查项的多对多的关联关系，操作t_checkgroup_checkitem表
//        获取当前存入t_checkgroup_checkitem表数据的id
        setCheckitemAndCheckGroup(checkGroup, checkitemIds);
    }

    private void setCheckitemAndCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
        Integer checkGroupId = checkGroup.getId();
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkGroupId", checkGroupId);
                map.put("checkitemId", checkitemId);
                checkGroupDao.setCheckitemAndCheckGroup(map);
            }
        }
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用mybatis的分页助手插件
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> page = checkGroupDao.findPage(queryString);
        long total = page.getTotal();
        List<CheckGroup> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIdAndCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdAndCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //更新检查组基本数据，操作t_checkgroup表
        checkGroupDao.update(checkGroup);
        //根据检查组id删除中间表数据（清理原有关联关系）
        checkGroupDao.deleteAssociation(checkGroup.getId());
        //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
        setCheckitemAndCheckGroup(checkGroup, checkitemIds);
    }

    @Override
    public void delete(Integer id) {
        //查询并判断当前信息是否偶外键关联
        long countByCheckgroupId = checkGroupDao.findCountByCheckgroupId(id);
        if (countByCheckgroupId > 0) {
            //如果有关联就抛异常
            throw new RuntimeException();
        }
        //删除中间表的关联信息
        checkGroupDao.deleteCountBySertmealIds(id);
        //删除当前信息，操作t_checkgroup表
        checkGroupDao.deleteCheckgroupId(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> list = checkGroupDao.findAll();
        return list;
    }
}
